//web联网，强制门户，保存wifi信息和城市信息
//使用高德免费API，申请key在      https://lbs.amap.com/api/webservice/guide/api/weatherinfo/#t1
//语音查询时间、日期、今日天气、明日天气
//调试完成——20240728

//esp12E与ASR01通过各自软串口连接
//esp  GPIO2 ——> ASR   IO6
       GPIO5 ——> ASR   IO7



#include<SoftwareSerial.h>
#include "NTPcode.h"
#include "WebConfig.h"
#include "GetWeather.h"

SoftwareSerial espSerial(5, 2);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  espSerial.begin(9600);
  if (WIFI_init()) {
    NTP_init();
  }
}
void loop() {
  server.handleClient();
  dnsServer.processNextRequest();

  int years, months, dd, hh, mm, ss, weekdays;
  if (espSerial.available() > 0) {
    String incomingData = espSerial.readStringUntil('\n'); // 读取直到换行符

    // 去除可能的换行符或回车符
    incomingData.trim();

    // 使用equals()方法判断字符串是否为"time"
    if (incomingData.equals("time")) {
      Serial.println("接收到的字符串是: time");
      hh = hour();
      mm = minute();
      espSerial.print("100");//查询时间标识100
      espSerial.print(hh); espSerial.print("A");
      espSerial.print(mm); espSerial.println("B");
    }
    else if (incomingData.equals("date")) {
      years = year();
      months = month();
      dd = day();
      weekdays = weekday();
      delay(1);
      espSerial.print("101");//查询日期标识101
      espSerial.print(years); espSerial.print("A");
      espSerial.print(months); espSerial.print("B");
      espSerial.print(dd); espSerial.print("C");
      espSerial.print(weekdays); espSerial.println("D");
    }
    else if (incomingData.equals("weat")) {
      GetWeatherLive();
      GetWeatherForcast();
      delay(1);
      espSerial.print("102");//查询今天天气标识102
      espSerial.print(WeatherCode(WeatherLive.weather)); espSerial.print("A");
      espSerial.print(Forcasts_0.daytemp); espSerial.print("B");
      espSerial.print(Forcasts_0.nighttemp); espSerial.print("C");
      espSerial.print(WinddirectionCode(WeatherLive.winddirection)); espSerial.print("D");
      espSerial.print(WeatherLive.windpower); espSerial.print("E");
      espSerial.print(WeatherLive.humidity); espSerial.println("F");
    }
    else if (incomingData.equals("tomo")) {
      GetWeatherForcast();
      delay(1);
      espSerial.print("103");//查询今天天气标识103
      espSerial.print(WeatherCode(Forcasts_1.dayweather)); espSerial.print("A");
      espSerial.print(Forcasts_1.daytemp); espSerial.print("B");
      espSerial.print(Forcasts_1.nighttemp); espSerial.print("C");
      espSerial.print(WinddirectionCode(Forcasts_1.winddirection)); espSerial.print("D");
      espSerial.print(Forcasts_1.windpower); espSerial.println("E");
    }
    else {
      // 如果不是以上，打印接收到的内容
      Serial.print("无法识别，接收到的字符串是: ");
      Serial.println(incomingData);
      espSerial.println("what");
    }
  }
}
